package com.salary.web;

import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.common.util.DTOResponse;
import com.common.util.ExtjsPageable;
import com.salary.entity.SalaryRecord;
import com.salary.entity.dto.SalaryRecordDTO;
import com.salary.entity.dto.monthDTO;
import com.salary.entity.dto.yearDTO;
import com.salary.service.ISalaryRecordService;
import com.staff.entity.Staff;

@Controller
@RequestMapping("/salaryRecord")
public class SalaryRecordController {
	@Autowired
	private ISalaryRecordService salaryRecordService;
	
	@RequestMapping("/findAll")
	public @ResponseBody List<SalaryRecordDTO> findAll() {
		List<SalaryRecord> list = salaryRecordService.findAll();
		List<SalaryRecordDTO> srDTO = new ArrayList();
		for(SalaryRecord sr:list) {
			//获取员工与记录
			Staff staff = salaryRecordService.findStaff(sr);
			if(staff == null)return null;
			SalaryRecordDTO dto = new SalaryRecordDTO(sr, salaryRecordService.findBasicSalary(sr).getAmount(), staff.getStaffName(),staff.getStaffNum());
			srDTO.add(dto);
		}
		return srDTO;
		
	}
	@RequestMapping("/findYears")
	public @ResponseBody List<yearDTO> findYears(){
		try {
			List<yearDTO> dtos = new ArrayList<yearDTO>();
			for(Integer year:salaryRecordService.getYears()) {
				dtos.add(new yearDTO(year));
			}
			return dtos;
		} catch (Exception e) {
			e.printStackTrace();
			return null;
		}
	}
	@RequestMapping("/findMonths")
	public @ResponseBody List<monthDTO> findMonths(Integer year){
		try {
			List<monthDTO> dtos = new ArrayList<monthDTO>();
			for(Integer month:salaryRecordService.getMonthsByYear(year)) {
				dtos.add(new monthDTO(month));
			}
			return dtos;
		} catch (Exception e) {
			e.printStackTrace();
			return null;
		}
	}
	@RequestMapping("/findPage")
	public @ResponseBody Page<SalaryRecordDTO> findPage(String staffName,String staffNum,Integer year,Integer month,ExtjsPageable pageable){
		try {
			System.out.println("员工编号："+staffNum);
			return salaryRecordService.findPage(staffName, staffNum, year, month, pageable.getPageable());
		} catch (Exception e) {
			return null;
		}
		
	}
}
